package com.meilishuo.android.performance;

import java.io.BufferedReader;
import java.io.File;
import java.io.InputStreamReader;

/**
 * Created by Xuemeng Wang on 15/3/19.
 */
public class ShellUtil {

    /**
     * 执行shell脚本
     */
    /**
     * 执行shell脚本
     */
    public static String execShellFile(String shellFile, String sourceFile)
    {
        if(null == shellFile || "".equals(shellFile))
            return null;
        //取得shellFile中shell文件，即去掉参数
        String shellFileTemp = shellFile;
        if(shellFile.contains(" "))
            shellFileTemp = shellFile.substring(0, shellFile.indexOf(" "));
        File file = new File(shellFileTemp);
        if(!file.exists() || !file.isFile())
            return null;
        Process ps = null;
        StringBuffer sb = new StringBuffer();
        try {
            ps = Runtime.getRuntime().exec(shellFile + " "+sourceFile);
            ps.waitFor();

            BufferedReader br = new BufferedReader(new InputStreamReader(ps.getInputStream()));

            String line;
            while ((line = br.readLine()) != null) {
                sb.append(line).append("\n");
            }
        } catch (Exception e1) {
            e1.printStackTrace();
        }
        return sb.toString();
    }
}
